package controller;

import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    /*
    参数User其实是一个Command对象
    类似于struts1中的ActionForm对象
    因为SpringMVC中Command对象不需要继承或实现任何东西
    这里将尸体对象当做Command对象使用

    User不是实体类
    相当于封装表单数据的一个类型
    作用
        返回数据
        接收数据
     */
    @RequestMapping("/show")
    public String show(User user){
        System.out.println("UserController.show()");
        System.out.println("user = " + user);
        user.setUsername("abc");
        user.setPassword("123");
        user.setPhone("12345678");
        user.setAge(23);
        return "user";
    }

//    @RequestMapping("/submit")
//    public String submit(User user){
//        System.out.println("UserController.submit()");
//        System.out.println("user = " + user);
//        System.out.println("user.getUsername() = " + user.getUsername());
//        System.out.println("user.getPassword() = " + user.getPassword());
//        System.out.println("user.getPhone() = " + user.getPhone());
//        System.out.println("user.getAge() = " + user.getAge());
//        return "redirect:/user/show";
//    }


//    @RequestMapping("/submit")
//    public String submit(User user, Errors errors){
//        System.out.println("UserController.submit()");
//        if(errors.hasErrors()){
//            System.out.println("UserController.submit().hasErrors()");
//            return "user";
//        }
//        System.out.println("user = " + user);
//        System.out.println("user.getUsername() = " + user.getUsername());
//        System.out.println("user.getPassword() = " + user.getPassword());
//        System.out.println("user.getPhone() = " + user.getPhone());
//        System.out.println("user.getAge() = " + user.getAge());
//        return "redirect:/user/show";
//    }

    @RequestMapping("/submit")
    public String submit(User user, BindingResult bindingResult){
        System.out.println("UserController.submit()");
        if(bindingResult.hasErrors()){
            System.out.println("UserController.submit().hasErrors()");
            return "user";
        }
        System.out.println("user = " + user);
        System.out.println("user.getUsername() = " + user.getUsername());
        System.out.println("user.getPassword() = " + user.getPassword());
        System.out.println("user.getPhone() = " + user.getPhone());
        System.out.println("user.getAge() = " + user.getAge());
        return "redirect:/user/show";
    }




}
